package com.lovo.middleman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.middleman.bean.StockBean;
import com.lovo.middleman.bean.StockPageBean;
import com.lovo.middleman.mapper.StockMapper;
import com.lovo.middleman.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, StockBean> implements IStockService {

    @Autowired
    StockMapper stockMapper;

    @Override
    public List<StockBean> queryStockBean() {
        QueryWrapper<StockBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("stock_id",0);
        return stockMapper.selectList(queryWrapper);
    }

    @Override
    public void addStockBean(StockBean stockBean) {
        List<StockBean> stockBeans = stockMapper.selectList(null);
        for (StockBean bean : stockBeans) {
            if (!bean.getGoodsName().equals(stockBean.getGoodsName())){
                stockMapper.insert(stockBean);
            }else {
                bean.setNumber(stockBean.getNumber());
                stockMapper.updateById(bean);
            }
        }
    }

    @Override
    public void updateStockBean(StockBean stockBean) {
        stockMapper.updateById(stockBean);
    }

    @Override
    public StockBean queryStockBeanById(int id) {
        return  stockMapper.selectById(id);
    }

    @Override
    public Page<StockBean> queryStockBeanByItem(int curPage,
                                                int pageSize,
                                                String goodsName,
                                                String goodsType) {
        QueryWrapper<StockBean> queryWrapper = new QueryWrapper<>();
        if (goodsName!=null &&!"".equals(goodsName)){
            queryWrapper.like("goods_name",goodsName);
        }
        if (goodsType!=null &&!"".equals(goodsType)){
            queryWrapper.like("goods_type",goodsType);
        }
       return stockMapper.selectPage(new Page<>(curPage,pageSize),queryWrapper);
    }

    @Override
    public Page<StockBean> queryStockPageBean(StockPageBean<StockBean> stockPageBean) {
        QueryWrapper<StockBean> queryWrapper  = new QueryWrapper<>();
        if (stockPageBean.getT().getGoodsName()!=null&&!"".equals(stockPageBean.getT().getGoodsName())){
            queryWrapper.like("goods_name",stockPageBean.getT().getGoodsName());
        }
        if (stockPageBean.getT().getMarketingPrice()!=0){
            queryWrapper.like("marketing_price",stockPageBean.getT().getMarketingPrice());
        }
        if (stockPageBean.getT().getGoodsType()!=null&&!"".equals(stockPageBean.getT().getGoodsType())){
            queryWrapper.like("goods_type",stockPageBean.getT().getGoodsType());
        }
        if (stockPageBean.getStartNumber()!=0){
            queryWrapper.ge("number",stockPageBean.getStartNumber());
        }
        if (stockPageBean.getEndNumber()!=0){
            queryWrapper.le("number",stockPageBean.getEndNumber());
        }
        return  stockMapper.selectPage(new Page<>(stockPageBean.getCurPage(),stockPageBean.getPageSize()),queryWrapper);

    }

    @Override
    public void updateStockPriceById(int id, double marketingPrice) {
        StockBean stockBean = stockMapper.selectById(id);
        stockBean.setMarketingPrice(marketingPrice);
        stockMapper.updateById(stockBean);
    }
}
